/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.record.service.impl;

import me.zhengjie.modules.app.record.domain.RecordDetailInfo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.record.repository.RecordDetailInfoRepository;
import me.zhengjie.modules.app.record.service.RecordDetailInfoService;
import me.zhengjie.modules.app.record.service.dto.RecordDetailInfoDto;
import me.zhengjie.modules.app.record.service.dto.RecordDetailInfoQueryCriteria;
import me.zhengjie.modules.app.record.service.mapstruct.RecordDetailInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.SecurityUtils;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.time.LocalDateTime;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Haojie Li
* @date 2021-03-03
**/
@Service
@RequiredArgsConstructor
public class RecordDetailInfoServiceImpl implements RecordDetailInfoService {

    private final RecordDetailInfoRepository recordDetailInfoRepository;
    private final RecordDetailInfoMapper recordDetailInfoMapper;

    @Override
    public Map<String,Object> queryAll(RecordDetailInfoQueryCriteria criteria, Pageable pageable){
        Page<RecordDetailInfo> page = recordDetailInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recordDetailInfoMapper::toDto));
    }

    @Override
    public List<RecordDetailInfoDto> queryAll(RecordDetailInfoQueryCriteria criteria){
        return recordDetailInfoMapper.toDto(recordDetailInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecordDetailInfoDto findById(String id) {
        RecordDetailInfo recordDetailInfo = recordDetailInfoRepository.findById(id).orElseGet(RecordDetailInfo::new);
        ValidationUtil.isNull(recordDetailInfo.getId(),"RecordDetailInfo","id",id);
        return recordDetailInfoMapper.toDto(recordDetailInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecordDetailInfoDto create(RecordDetailInfo resources) {
        resources.setId(IdUtil.simpleUUID()); 
        resources.setCreateBy(SecurityUtils.getCurrentUsername());
        resources.setCreateTime(LocalDateTime.now());
        resources.setIsEnable(1);
        return recordDetailInfoMapper.toDto(recordDetailInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecordDetailInfo resources) {
    	resources.setUpdateBy(SecurityUtils.getCurrentUsername());
    	resources.setUpdateTime(LocalDateTime.now());
    	
        RecordDetailInfo recordDetailInfo = recordDetailInfoRepository.findById(resources.getId()).orElseGet(RecordDetailInfo::new);
        ValidationUtil.isNull( recordDetailInfo.getId(),"RecordDetailInfo","id",resources.getId());
        recordDetailInfo.copy(resources);
        recordDetailInfoRepository.save(recordDetailInfo);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            recordDetailInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecordDetailInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecordDetailInfoDto recordDetailInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备IP", recordDetailInfo.getIp());
            map.put("设备MAC", recordDetailInfo.getMac());
            map.put("酒店ID", recordDetailInfo.getHotelId());
            map.put("酒店名称", recordDetailInfo.getHotelName());
            map.put("酒店房间ID", recordDetailInfo.getHotelRoomId());
            map.put("酒店房间号", recordDetailInfo.getHotelRoomNo());
            map.put("开机时间", recordDetailInfo.getOpenTime());
            map.put("关机时间", recordDetailInfo.getCloseTime());
            map.put("开关机时间差结果", recordDetailInfo.getTimeResult());
            map.put(" createBy",  recordDetailInfo.getCreateBy());
            map.put(" updateBy",  recordDetailInfo.getUpdateBy());
            map.put(" createTime",  recordDetailInfo.getCreateTime());
            map.put(" updateTime",  recordDetailInfo.getUpdateTime());
            map.put(" isEnable",  recordDetailInfo.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}